/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2024-03-26
 * Time: 16:25
 */
public class MySingleList2 {
    class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;

    public void creatList() {
        MySingleList2.ListNode node1 = new MySingleList2.ListNode(12);
        MySingleList2.ListNode node2 = new MySingleList2.ListNode(23);
        MySingleList2.ListNode node3 = new MySingleList2.ListNode(34);
        MySingleList2.ListNode node4 = new MySingleList2.ListNode(45);
        MySingleList2.ListNode node5 = new MySingleList2.ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        this.head = node1;
    }

    //遍历单链表
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    ////得到单链表的长度 --》链表中节点的个数
    public int size() {
        ListNode cur = head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }
    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            return;
        }
        ListNode cur = head;
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index, int data) {
        int len = size();
        if(index < 0 || index > len) {
            throw new IndexOutOfBounds("任意位置index不合法,此时index为: " + index);
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == len) {
            addLast(data);
            return;
        }
        //找到要插入的前一个节点
        ListNode cur = searchPreNode(index);
        ListNode node = new ListNode(data);
        //进行插入
        node.next = cur.next;
        cur.next = node;
    }
    private ListNode searchPreNode(int index) {
        ListNode cur = head;
        while((index - 1) > 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(head == null) {
            return;
        }
        if(head.val == key) {
            head = head.next;
        }
        ListNode prev = searchPrev(key);
        if(prev.next == null) {
            System.out.println("没有你要删除的关键字");
            return;
        }
        ListNode del = prev.next;
        prev.next = del.next;
    }
    //找到关键字key的前一个结点
    private ListNode searchPrev(int key) {
        ListNode prev = head;
        while(prev.next != null) {
            if(prev.next.val == key) {
                return prev;
            }
            prev = prev.next;
        }
        return prev;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }
        ListNode prev = head;
        ListNode cur = prev.next;
        while(cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if(head.val == key) {
            head = head.next;
        }

    }

    public void clear() {
        while(head != null) {
            ListNode headNext = head.next;
            headNext = null;
            head = headNext;
        }
    }
}
